/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PatientDetail.java
 *
 * Created on Aug 28, 2011, 8:42:49 AM
 */
package patientManager;

import app.Global;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Fun
 */
public class PatientDetail extends java.awt.Dialog {

    int id;
    Vector vData;
    Vector vColumn;
    Vector vData1;
    Vector vColumn1;
    PatientTab parent;
    String name;
    private int emp_id;

    /** Creates new form PatientDetail */
    public PatientDetail(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.parent = (PatientTab) parent;
        initComponents();
    }

    PatientDetail(PatientTab aThis, boolean b, int id) {
        super(aThis, b);
        this.id = id;
        initComponents();
        initPatientDetail();
        initSchedule();
        this.setVisible(true);
    }

    void initPatientDetail() {

        Connection cn = Global.getConnection();
        Statement st = null;
        ResultSet rs = null;

        try {
            st = cn.createStatement();
            rs = st.executeQuery("SELECT firstname, lastname, address, mobile, email, note, created, modified"
                    + " FROM patients WHERE id = " + id);

            while (rs.next()) {
                name = rs.getString(1) + rs.getString(2);
                lblPatientName.setText(name);
                lblAddress.setText(rs.getString(3));
                lblMobile.setText(rs.getString(4));
                lblEmail.setText(rs.getString(5));
                lblCreated.setText(rs.getString(7));
                lblModified.setText(rs.getString(8));
                lblNote.setText(rs.getString(6));
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                st.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    void initSchedule() {
        vData = new Vector();
        vColumn = new Vector();
        Connection cn = Global.getConnection();
        Statement st = null;
        ResultSet rs = null;
        try {
            st = cn.createStatement();
            rs = st.executeQuery(
                    " SELECT     patient_schedules.id, employees.first_name, employees.last_name as employee_name, patient_schedules.scheduled_time, patient_schedules.note, "
                    + " patient_schedules.status, patient_schedules.created, patient_schedules.modified, employees.id"
                    + " FROM         employees INNER JOIN "
                    + " patient_schedules ON employees.id = patient_schedules.employee_id INNER JOIN "
                    + " patients ON patients.id = patient_schedules.patient_id "
                    + " where patients.id  = " + id
                    + " ORDER BY patient_schedules.scheduled_time");
            ResultSetMetaData rsmt = rs.getMetaData();
            for (int i = 1; i < rsmt.getColumnCount(); i++) {
                if (i == 2) {
                    vColumn.add("DoctorName");
                    i++;
                } else {
                    vColumn.add(rsmt.getColumnName(i));
                }
            }
            while (rs.next()) {
                Vector vRow = new Vector();
                for (int i = 1; i < rsmt.getColumnCount(); i++) {
                    if (i == 2) {
                        vRow.add(rs.getString(i) + " " + rs.getString(i + 1));
                        i++;
                    } else {
                        vRow.add(rs.getString(i));
                    }
                }
                emp_id = rs.getInt(rsmt.getColumnCount());
                vData.add(vRow);
            }
            jTable1.setModel(new DefaultTableModel(vData, vColumn));
        } catch (SQLException ex) {
            Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                rs.close();
                st.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pumStatus = new javax.swing.JPopupMenu();
        miStatus1 = new javax.swing.JMenuItem();
        miStatus2 = new javax.swing.JMenuItem();
        miStatus3 = new javax.swing.JMenuItem();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        lblPatientName = new javax.swing.JLabel();
        lblAddress = new javax.swing.JLabel();
        lblMobile = new javax.swing.JLabel();
        lblEmail = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lblCreated = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        lblModified = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        lblNote = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        miStatus1.setText("1");
        miStatus1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miStatus1ActionPerformed(evt);
            }
        });
        pumStatus.add(miStatus1);

        miStatus2.setText("2");
        miStatus2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miStatus2ActionPerformed(evt);
            }
        });
        pumStatus.add(miStatus2);

        miStatus3.setText("3");
        miStatus3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                miStatus3ActionPerformed(evt);
            }
        });
        pumStatus.add(miStatus3);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jLabel1.setText("Patient Name :");

        jLabel2.setText("Address :");

        jLabel3.setText("Mobile :");

        jLabel4.setText("Email :");

        lblPatientName.setText("jLabel1");

        lblAddress.setText("jLabel1");

        lblMobile.setText("jLabel1");

        lblEmail.setText("jLabel1");

        jLabel9.setText("Created :");

        lblCreated.setText("jLabel1");

        jLabel11.setText("Modified :");

        lblModified.setText("jLabel1");

        jLabel13.setText("Note :");

        lblNote.setText("jLabel1");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable1MouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable1);

        jButton1.setText("Add New Patient Schedule");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(35, 35, 35)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3))
                        .addGap(49, 49, 49)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(lblEmail, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblPatientName, javax.swing.GroupLayout.DEFAULT_SIZE, 96, Short.MAX_VALUE)
                            .addComponent(lblAddress, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(lblMobile, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(157, 157, 157)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel11)
                            .addComponent(jLabel13))
                        .addGap(67, 67, 67)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblNote)
                            .addComponent(lblModified)
                            .addComponent(lblCreated)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 861, Short.MAX_VALUE)))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(586, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(136, 136, 136))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(lblPatientName)
                    .addComponent(jLabel9)
                    .addComponent(lblCreated))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(lblAddress)
                        .addComponent(jLabel11)
                        .addComponent(lblModified)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(lblMobile))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(lblEmail)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel13)
                        .addComponent(lblNote)))
                .addGap(4, 4, 4)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 78, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 251, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(120, 120, 120))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Vector v = (Vector) vData.lastElement();
        int stt = Integer.parseInt(v.get(4).toString());
        new PatientSchedule(parent, true, name, id, emp_id, stt);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jTable1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable1MouseClicked
        // TODO add your handling code here:
        pumStatus.show(evt.getComponent(), evt.getX(), evt.getY());
    }//GEN-LAST:event_jTable1MouseClicked

    private void miStatus1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miStatus1ActionPerformed
        // TODO add your handling code here:
        Connection cn = Global.getConnection();
        Statement st = null;
        Vector v = (Vector) vData.get(jTable1.getSelectedRow());
        int pid = Integer.parseInt(v.get(0).toString());
        try {st = cn.createStatement();
            Date todayD = new Date(System.currentTimeMillis());
            SimpleDateFormat dayFormat = Global.getDefaultDateFormat();
            String todayS = dayFormat.format(todayD.getTime());
            int i = st.executeUpdate("UPDATE patient_schedules SET status = 1, modified = '" + todayS + "' WHERE id = " + pid);
            if (i > 0) {
                JOptionPane.showMessageDialog(this, "SUCCESSFUL");
            } else {
                JOptionPane.showMessageDialog(this, "UNSUCCESSFUL");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        initSchedule();
    }//GEN-LAST:event_miStatus1ActionPerformed

    private void miStatus2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miStatus2ActionPerformed
        // TODO add your handling code here:
        Connection cn = Global.getConnection();
        Statement st = null;
        try {
            Vector v = (Vector) vData.get(jTable1.getSelectedRow());
            int id = Integer.parseInt(v.get(0).toString());
            st = cn.createStatement();
            Date todayD = new Date(System.currentTimeMillis());
            SimpleDateFormat dayFormat = Global.getDefaultDateFormat();
            String todayS = dayFormat.format(todayD.getTime());
            int i = st.executeUpdate("UPDATE patient_schedules SET status = 2, modified = '" + todayS + "' WHERE id = " + id);
            if (i > 0) {
                JOptionPane.showMessageDialog(this, "SUCCESSFUL");
            } else {
                JOptionPane.showMessageDialog(this, "UNSUCCESSFUL");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        initSchedule();
    }//GEN-LAST:event_miStatus2ActionPerformed

    private void miStatus3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miStatus3ActionPerformed
        // TODO add your handling code here:
        Connection cn = Global.getConnection();
        Statement st = null;
        try {
            Vector v = (Vector) vData.get(jTable1.getSelectedRow());
            int pid = Integer.parseInt(v.get(0).toString());
            st = cn.createStatement();
            Date todayD = new Date(System.currentTimeMillis());
            SimpleDateFormat dayFormat = Global.getDefaultDateFormat();
            String todayS = dayFormat.format(todayD.getTime());
            int i = st.executeUpdate("UPDATE patient_schedules SET status = 3, modified = '" + todayS + "' WHERE id = " + pid);
            if (i > 0) {
                JOptionPane.showMessageDialog(this, "SUCCESSFUL");
            } else {
                JOptionPane.showMessageDialog(this, "UNSUCCESSFUL");
            }
        } catch (SQLException ex) {
            Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
                cn.close();
            } catch (SQLException ex) {
                Logger.getLogger(PatientDetail.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        initSchedule();
    }//GEN-LAST:event_miStatus3ActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JLabel lblAddress;
    private javax.swing.JLabel lblCreated;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblMobile;
    private javax.swing.JLabel lblModified;
    private javax.swing.JLabel lblNote;
    private javax.swing.JLabel lblPatientName;
    private javax.swing.JMenuItem miStatus1;
    private javax.swing.JMenuItem miStatus2;
    private javax.swing.JMenuItem miStatus3;
    private javax.swing.JPopupMenu pumStatus;
    // End of variables declaration//GEN-END:variables
}
